<?php
class CategoryModel extends  CommonModel {
    public $catalogList=array();

    protected $_auto = array(
        array('create_time','time',1,'function'),
        array('update_time','time',2,'function'),
        array('user_id','getUser',3,'callback'),
    );

    public $_validate	=	array(
		array('name','require','名称必须填写！'),
		);

 /**
     * 得到$pid下的所有分类，$pid默认为0,即根目录
     *$type 1,表示系统类别;2表示所有类别,3表示当前用户类别,4表示系统分类和自己的分类
     */
    function getAllCatalog($pid=0,$level=0,$stat=1) {
        for($j=0;$j<$level;$j++) {
            $level_str .= '&nbsp;&nbsp;&nbsp;';
        }
        if($level) {
            $level_str .= '┗';
        }
        $condition = array();
        $condition['parent_id'] = $pid;
        if ($stat==1)
        {
         $condition['status'] = 1;
        }
        $rs = $this->where($condition)->field('id,name,status,sort')->order('parent_id asc,sort asc')->select();
        for($i=0;$i<count($rs);$i++ ) {
            $rs[$i]['level'] = $level_str;
            $this->catalogList[] = $rs[$i];
            $this->getAllCatalog($rs[$i]['id'],$level+1,$stat);
        }
        return $this->catalogList;
    }

    public function getUser(){
        return $_SESSION[C('USER_AUTH_KEY')];
    }

}
?>
